// pages/rebackOrder/rebackOrder.js
const app = getApp();
import { reorderlist, modalIds } from '../../config/https.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNone: true, // 是否为空
    noMore: false, // 没有更多
    status: 0, // 订单状态 0全部 1审核中 2同意 3拒绝 4已取消
    list: [], // 列表
    page: 0,
    pagesize: 6,
    modals: [], // 消息提示模板
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    modalIds({
      index: 1
    }).then(res => {
      if (res.code == 1) {
        wx.getSetting({
          withSubscriptions: true,
          success(data) {
            let text = res.data[0];
            if (!data.subscriptionsSetting.text) {
              // that.sureModals();
            }
          }
        })
        this.setData({
          modals: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
    this.getData();
  },

  // 获取数据
  getData() {
    app.loading();
    let that = this;
    reorderlist({
      token: app.globalData.userId,
      refund_status: that.data.status,
      page: ++this.data.page,
      page_size: this.data.pagesize
    }).then(res =>{
      if(res.code == 1){
        wx.hideLoading();
        if(res.data.list.length > 0){
          that.setData({
            list: [...that.data.list,...res.data.list],
            noMore: Number(res.data.list.length) >= Number(that.data.pagesize) ? false : true
          })
        }

        if(that.data.list.length > 0){
          that.setData({
            isNone: false
          })
        }else{
          that.setData({
            isNone: true
          })
        }
      }else{
        this.setData({
          isNone: true
        })
        app.$tip(res.msg);
      }
    })
  },

  // 格式数据
  initData(){
    this.setData({
      list: [],
      page: 0,
      isNone: false,
      noMore: false,
    })
  },

  // 获取授权信息
  sureModals() {
    let that = this;
    wx.showModal({
      title: '温馨提示',
      content: '华为全屋定制需要您授权接收订阅消息',
      confirmColor: '#57b127',
      success(res) {
        if (res.confirm) {
          wx.requestSubscribeMessage({
            tmplIds: that.data.modals.slice(0, 3),
            success(data) {
            }
          })
        }
      }
    })

  },

  // 改变订单状态
  changeStatus(e) {
    this.setData({
      status: e.currentTarget.dataset.type,
      list: [],
      page: 0,
    })
    this.getData();
  },

  // 去订单详情
  toDetail(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/reorderDetail/reorderDetail?id='+ id,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.noMore){
      this.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})